#include "AccusationCardsDialog.h"

#include <boost/bind.hpp>
#include <SFML/Graphics.hpp>
#include "../CardsGroup.hpp"
#include "../ColoursPalette.h"
#include "../../../../utils/gui/Button.h"
#include "../../../../utils/gui/Label.h"


using namespace Views::Helpers::Graphic::Accusation;
using namespace Utils;

AccusationCardsDialog::AccusationCardsDialog(Cards::CardsCollection cards)
	:player_card(NULL), weapon_card(NULL), room_card(NULL)
{
	PlayersCardsGroup* players_cards_group = new PlayersCardsGroup(cards.getPlayersCards(), true);
	WeaponsCardsGroup* weapons_cards_group = new WeaponsCardsGroup(cards.getWeaponsCards(), true);
	RoomsCardsControl* rooms_cards_group = new RoomsCardsControl(cards.getRoomsCards(), true);
	
	players_cards_group->Move(10, 20);
	weapons_cards_group->Move(10 + players_cards_group->GetWidth() + 10, 20);
	rooms_cards_group->Move(10 + players_cards_group->GetWidth() + 10 + players_cards_group->GetWidth() + 10, 20);

	addChild(players_cards_group);
	addChild(weapons_cards_group);
	addChild(rooms_cards_group);

	players_cards_group->doOnCardPick(boost::bind(&AccusationCardsDialog::setCard, this, (Cards::Card**) &player_card, _1));
	weapons_cards_group->doOnCardPick(boost::bind(&AccusationCardsDialog::setCard, this, (Cards::Card**) &weapon_card, _1));
	rooms_cards_group->doOnCardPick(boost::bind(&AccusationCardsDialog::setCard, this, (Cards::Card**) &room_card, _1));

	int width = 40 + players_cards_group->GetWidth() + weapons_cards_group->GetWidth()
		+ rooms_cards_group->GetWidth();

	const int height = std::max(players_cards_group->GetHeight(), std::max(weapons_cards_group->GetHeight(),
		rooms_cards_group->GetHeight()));

	GUI::Button* OKButton = new GUI::Button();
	OKButton->SetShape(sf::Shape::Rectangle(0, 0, 80, 30, Colours::LightAlt));
	OKButton->SetText(GUI::Label(L"Sprawdzam"));
	OKButton->Move(width - 90, 20 + height + 5);
	addChild(OKButton);

	OKButton->doOnClick(boost::bind(&AccusationCardsDialog::submitCards, this));

	Shape = sf::Shape::Rectangle(0, 0, static_cast<float>(width), static_cast<float>(20 + height + 5 + OKButton->GetHeight() + 5),
		Colours::Medium);
}

boost::signals::connection AccusationCardsDialog::doOnCardsSet(const OnCardsSet::slot_function_type& slot)
{
	return cardsSetEvent.connect(slot);
}

void AccusationCardsDialog::submitCards()
{
	if (player_card == NULL || weapon_card == NULL || room_card == NULL)
	{

	}
	else
	{
		Cards::CardSet cards;
		cards.add(player_card);
		cards.add(weapon_card);
		cards.add(room_card);
		cardsSetEvent(cards);
	}
}

void AccusationCardsDialog::setCard(Cards::Card** dest, Cards::Card* src)
{
	*dest = src;
}
